import {
  getEasRun
} from "../../services/diagnosis"
import {
  getSexName,
  formatDateToZ,
  // countResult
} from "../../utils/util"

Page({
  data: {
    isLoad: false,//是否显示诊断加载中
    users_id: wx.getStorageSync('users_id')
  },
  onLoad() {
    wx.setStorageSync('operate_step', 3)
  },
  async setNavigationBar(title, frontColor, backgroundColor) {
    await wx.p.setNavigationBarTitle({
      title: title
    })

    await wx.setNavigationBarColor({
      frontColor: frontColor,
      backgroundColor: backgroundColor
    });
  },
  onPayBtnTap() {
    this.setNavigationBar('诊断中', '#000000', '#ffffff')
    this.setData({
      isLoad: true
    })
    this.data.requestInterval = setInterval(async () => {
      this.getEasRunRequst()
    }, wx.getStorageSync('infer_time'));
  },
  getEasRunRequst() {
    getEasRun((data) => {
      console.log(data)
      setTimeout(() => {
        const {
          user,
          handPic
        } = data
        const {
          username,
          sex,
          birth_solar,
          created_at
        } = user
        let birthArr = birth_solar.split("-")
        console.log(birthArr)
        // let reportContent = index + ". 您的先天弱脏为" + countResult(birthArr[0], birthArr[1], birthArr[2]).replaceAll("、", "")
        const resultList = handPic.map(item => item.infer_result)
        let index = 1
        let reportContent = ""
        for (let i in resultList) {
          // if(!resultList[i]) continue
          reportContent += "#+#reportContent_" + (index++) + "#-#" + resultList[i]
        }
        clearInterval(this.data.requestInterval)
        wx.removeStorageSync('operate_step')
        wx.removeStorageSync('users_id')
        wx.redirectTo({
          url: `/pages/diagnosis-result/diagnosis-result?username=${username}&gender=${getSexName(sex)}&birthdate=${formatDateToZ(birth_solar)}&reportContent=${reportContent}&reportDate=${formatDateToZ(created_at.substring(0, 10))}`
        })
      }, 1000)
    }, (res) => {
      if (res.data.errcode !== -2) {
        wx.p.showModal({
          title: '请求失败',
          content: res.data.message,
          showCancel: false
        })
        this.setNavigationBar('支付', '#000000', '#855b38')
        this.setData({
          isLoad: false
        })
        clearInterval(this.data.requestInterval)
      }
    }, this.data.users_id)
  },
  onUnload() {//页面销毁
    this.data.requestInterval&&clearInterval(this.data.requestInterval)
    // wx.removeStorageSync('operate_step')
    // wx.removeStorageSync('users_id')
  }
})